#include "tetris_game.h"

namespace Tetris{

Tetris_Game::Tetris_Game(int left, int top, int width, int height, bool rightlayout,UI::UI_Window* parent) : 
	UI::UI_Window(UI::UI_Rect(left,top,width + left,height + top),parent),
	level(1),
	rightLayout(rightlayout)
{
	if(!rightLayout)
		this->p_ui_table = new Tetris_Table(533 - 400,5,this);
	else
		this->p_ui_table = new Tetris_Table(5,5,this);
	
	this->p_game = new Game();
	this->p_game->SetPlayer("黄坚");
	this->p_timer = new UI::UI_Timer(this);
	
	if(!rightLayout)
		p_ui_nextShape = new Tetris_NextShape(400 - 400,5,this);
	else
		p_ui_nextShape = new Tetris_NextShape(272,5,this);
	
	if(!rightLayout)
		p_ui_score = new Tetris_Score(400 - 400,142,this);
	else
		p_ui_score = new Tetris_Score(272,142,this);
	this->p_ui_score->ShowScore(this->p_game->GetScore()); 
	
	if(!rightLayout)
		p_ui_player = new Tetris_Player(400 - 400,260,this);
	else
		p_ui_player = new Tetris_Player(272,260,this);
	p_ui_player->ShowPlayer(this->p_game->GetPlayer());
	
	if(!rightLayout)
		p_ui_notice = new Tetris_Notice(400 - 400,400,this);
	else
		p_ui_notice = new Tetris_Notice(272,400,this);
	p_ui_notice->ShowNotice("游戏暂停");
	
	UI::UI_MessageServer::Instance().RegisterMessageClient(this, UI::UI_Message::CTRL_TIMER, UI::UI_MessageServer::PRIORITY_FIRST);
	UI::UI_MessageServer::Instance().RegisterMessageClient(this, UI::UI_Message::KEYBOARD_KEYDOWN, UI::UI_MessageServer::PRIORITY_FIRST);
}

void Tetris_Game::GameStateChange(){
	if(this->p_game){
		if(this->p_game->IsPause() || this->p_game->GameOver()){	
			this->p_timer->StopTimer();
			if(this->p_game->IsPause())
				this->p_ui_notice->ShowNotice("游戏暂停");
			if(this->p_game->GameOver())
				this->p_ui_notice->ShowNotice("游戏结束");
			return;
		}
		else{
			if(!this->p_timer->IsRunning()){
				this->p_timer->StartTimer(1000 - this->level * 100,true);
				this->p_ui_notice->ShowNotice("加油哈!");
			}
		}
		
		if(this->level < (this->p_game->GetScore() / 100 + 1) ){
			this->level = this->p_game->GetScore() / 100 + 1;
			this->p_timer->StartTimer(1000 - this->level * 100,true);
		}
	}
	
	if(this->p_ui_table)
		this->p_ui_table->DrawCurrentTable(this->p_game->GetCurrentState());
	if(this->p_ui_nextShape)
		this->p_ui_nextShape->SetShape(this->p_game->NextShape());
	if(this->p_ui_score)
		this->p_ui_score->ShowScore(this->p_game->GetScore());
}

void Tetris_Game::Draw(){
	this->GameStateChange();
	UI::UI_Window::Draw();
}


bool Tetris_Game::HandleMessage(UI::UI_Message* pMessage){
	bool bHandled = false;
	if(!bHandled && pMessage){
		switch(pMessage->MessageType()){
		case UI::UI_Message::KEYBOARD_KEYDOWN:{
			UI::UI_KeyboardMessage* p = dynamic_cast<UI::UI_KeyboardMessage*>(pMessage);
			switch(p->Key){
			case SDLK_w:
				if(!this->rightLayout)
					this->p_game->Transform(); 
				break;
			case SDLK_d:
				if(!this->rightLayout)
					this->p_game->MoveRight(); 
				break;
			case SDLK_s:
				if(!this->rightLayout)
					this->p_game->MoveDown();
				break;
			case SDLK_a:
				if(!this->rightLayout)
					this->p_game->MoveLeft();
				break;
			case SDLK_SPACE:
				if(!this->rightLayout)
					this->p_game->MoveBottom(); 
				break;
			case SDLK_UP:
				if(this->rightLayout)
					this->p_game->Transform(); 
				break;
			case SDLK_RIGHT:
				if(this->rightLayout)
					this->p_game->MoveRight(); 
				break;
			case SDLK_DOWN:
				if(this->rightLayout)
					this->p_game->MoveDown();
				break;
			case SDLK_LEFT:
				if(this->rightLayout)
					this->p_game->MoveLeft();
				break;
			case SDLK_RETURN:
				if(this->rightLayout)
					this->p_game->MoveBottom(); 
				break;
			case SDLK_r:
				this->p_game->Resume(); this->level = 1; this->p_timer->StartTimer(1000 - this->level * 100,true); break;
			case SDLK_p:
				this->p_game->TogglePause(); break;
			case SDLK_ESCAPE:
				UI::UI_MessageServer::Instance().QueueMessage(new UI::UI_Message(UI::UI_Message::APP_EXIT, 0, this));
				break;
			default:
				break;
			}
			break;
		}
		case UI::UI_Message::CTRL_TIMER:{
			if(pMessage->Destination() == this && pMessage->Source() == this->p_timer)
				this->p_game->MoveDown();
			break;
		}
		default:
			bHandled = UI::UI_Window::HandleMessage(pMessage);
			break;
		}
	}
	this->Draw();
	return bHandled;

}

}